﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tasker.Composition;
using Tasker.Infrastructure;
using Tasker.Infrastructure.Entities;
using Tasker.Infrastructure.Services;
using Tasker.WebUI.Models.Tasks;

namespace Tasker.WebUI.Controllers
{
    [Authorize]
    public class HomeController : TaskerControllerBase
    {
        private IAccountService _account = Resolver.Resolve<IAccountService>();
        private ITaskService _tasks = Resolver.Resolve<ITaskService>();

        public ActionResult Index()
        {
            IEnumerable<ITask> tasks = _tasks.SelectAll();
            TaskListShowModel model = new TaskListShowModel();
            model.Tasks = from task in tasks
                             select new TaskListElem()
                                        {
                                            Id = task.Id,
                                            Name = task.Name,
                                            StartDate = task.StartDate,
                                            FinishDate = task.FinishDate,
                                            TaskWorkerId = UserId(_tasks.GetTaskWorker(task.Id)),
                                            TaskWorkerFullName = UserFullName(_tasks.GetTaskWorker(task.Id)),
                                            AuthorId = task.AuthorId,
                                            AuthorFullName = UserFullName(_account.SelectUser(task.AuthorId))
                                        };
            return View(model);
        }

        private int UserId(IUser user)
        {
            return user == null ? 0 : user.Id;
        }

        public ActionResult Incoming()
        {
            IEnumerable<ITask> tasks = _tasks.SelectAllIncoming(CurrentUserId);
            TaskListShowModel model = new TaskListShowModel();
            model.Tasks = from task in tasks
                          select new TaskListElem()
                          {
                              Id = task.Id,
                              Name = task.Name,
                              StartDate = task.StartDate,
                              FinishDate = task.FinishDate,
                              TaskWorkerId = UserId(_tasks.GetTaskWorker(task.Id)),
                              TaskWorkerFullName = UserFullName(_tasks.GetTaskWorker(task.Id)),
                              AuthorId = task.AuthorId,
                              AuthorFullName = UserFullName(_account.SelectUser(task.AuthorId))
                          };
            return View("Index", model);
        }

        public ActionResult Outcoming()
        {
            IEnumerable<ITask> tasks = _tasks.SelectAllOutcoming(CurrentUserId);
            TaskListShowModel model = new TaskListShowModel();
            model.Tasks = from task in tasks
                          select new TaskListElem()
                          {
                              Id = task.Id,
                              Name = task.Name,
                              StartDate = task.StartDate,
                              FinishDate = task.FinishDate,
                              TaskWorkerId = UserId(_tasks.GetTaskWorker(task.Id)),
                              TaskWorkerFullName = UserFullName(_tasks.GetTaskWorker(task.Id)),
                              AuthorId = task.AuthorId,
                              AuthorFullName = UserFullName(_account.SelectUser(task.AuthorId))
                          };
            return View("Index", model);
        }

        public string UserFullName(IUser user)
        {
            if (user == null) return "";
            if (user.FirstName == null && user.LastName == null)
                return user.Login;
            return user.FirstName + " " + user.LastName;
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
